//
//  SMUserManager.m
//  DiDi
//
//  Created by huangtie on 2017/3/18.
//  Copyright © 2017年 Sinmore. All rights reserved.
//

#import "SMUserManager.h"
#import "ZBLoginViewController.h"
#import "SAMKeychain.h"

#define kKeychain_Key_Token @"token"
#define kKeychain_Key_UserInfo @"userinfo"
#define kKeychain_Key_Tel @"tel"

static NSString *kKeyChainService = @"SMChainService";

@implementation SMUser
MJCodingImplementation

@end

@implementation SMUserManager

+ (instancetype)shareManager {
    static SMUserManager *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[SMUserManager alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadToken];
    }
    return self;
}

- (SMUser *)userInfo
{
    if (!_userInfo) {
        _userInfo = [[SMUser alloc] init];
    }
    return _userInfo;
}

- (void)setToken:(NSString *)token
{
    _token = token;
    [self saveToken];
}

- (BOOL)isLogin
{
    return self.token.length;
}

- (BOOL)cheakLogin:(UIViewController *)targe
          callBack:(void (^)(NSString *token))callBack
       cancelBlock:(void (^)(void))cancelBlock
{
    if (self.token.length) {
        if (callBack) {
            callBack(self.token);
        }
        return YES;
    }else{
        [self login:targe callBack:callBack cancelBlock:cancelBlock];
        return NO;
    }
}

- (void)login:(UIViewController *)targe
     callBack:(void (^)(NSString *token))callBack
  cancelBlock:(void (^)(void))cancelBlock
{
    if (!targe){
        targe = [UIViewController visibleController];
    }
    ZBLoginViewController *loginVC = [[ZBLoginViewController alloc] init];
    loginVC.loginSuccessBlock = ^(NSString *token){
        if (callBack)
        {
            callBack(token);
        }
    };
    loginVC.cancelBlock = ^(void){
        if (cancelBlock) {
            cancelBlock();
        }
    };
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [targe presentViewController:nav animated:YES completion:nil];
}

- (void)logout:(void (^)(void))callBack
{
    self.userInfo = nil;
    self.token = @"";
    if (callBack) {
        callBack();
    }
}

- (void)reloadUserInformation:(void (^)(SMUser *info))callBack
{
    if (self.token) {
        
    }else{
        if (callBack) {
            callBack(nil);
        }
    }
}

- (void)loadToken
{
    self.token = [SAMKeychain passwordForService:kKeyChainService account:kKeychain_Key_Token];
}

- (void)saveToken
{
    [SAMKeychain setPassword:self.token forService:kKeyChainService account:kKeychain_Key_Token];
}

+ (void)saveTelString:(NSString *)tel
{
    [SAMKeychain setPassword:tel forService:kKeyChainService account:kKeychain_Key_Tel];
}

+ (NSString *)loadTelString
{
    return [SAMKeychain passwordForService:kKeyChainService account:kKeychain_Key_Tel];
}

+ (NSInteger)loadTimeDiffer
{
    NSDate *oldDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"CodeTimekey"];
    if (!oldDate) {
        return 0;
    }
    NSTimeInterval late = [oldDate timeIntervalSince1970] * 1;
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970] * 1;
    return now - late;
}

+ (void)saveSendCodeTime
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"CodeTimekey"];
}

+ (BOOL)isMobilePhoneNumber:(NSString *)mobileNum{
    if(mobileNum.length <= 0)return NO;
    //    NSString * regex = @"^1[3458][0-9]{9}$";
    //    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    //    return [pred evaluateWithObject:str];
    
    //    /**
    //     * 手机号码
    //     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    //     * 联通：130,131,132,152,155,156,185,186
    //     * 电信：133,1349,153,180,189
    //     * 170：17
    //     */
    //    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    //    /**
    //     * 中国移动：China Mobile
    //     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    //     */
    //    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    //    /**
    //     * 中国联通：China Unicom
    //     * 130,131,132,152,155,156,185,186
    //     */
    //    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    //    /**
    //     * 中国电信：China Telecom
    //     * 133,1349,153,180,189
    //     */
    //    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     * 1**号码段
     * 号码：17*
     */
    NSString * MOBILE = @"^[0-9]*$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    //    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    //    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    //    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    //    NSPredicate *regextestOther = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", OTHER];
    
    if ([regextestmobile evaluateWithObject:mobileNum] == YES)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

#define USERDEFAULT_KEY_OPEN_VER @"USERDEFAULT_KEY_OPEN_VER"
+ (BOOL)isFirstOpenApp
{
    NSString * versionNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
    NSString *savedVer = [[NSUserDefaults standardUserDefaults] stringForKey:USERDEFAULT_KEY_OPEN_VER];
    return ![versionNumber isEqualToString:savedVer];
}

+ (void)rejectFirstOpenApp
{
    NSString * versionNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    [[NSUserDefaults standardUserDefaults] setValue:versionNumber forKey:USERDEFAULT_KEY_OPEN_VER];
}

@end
